﻿using System;

using System.Data.SqlClient;
using System.Text;

namespace IFD2
{
    public partial class SurchargeSegs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["code"] == null || Request.QueryString["effective_time"] == null || Request.QueryString["flt_surcharge_types"] == null)
                {
                    lblError.Text = "Bad page calling!";
                }
                else
                {
                    lblSurchargeCode.Text = Request.QueryString["code"].ToString().Trim().ToUpper();
                    lblEffectiveTime.Text = Request.QueryString["effective_time"].ToString().Trim().ToUpper();
                    lblFltSurchargeTypes.Text = Request.QueryString["flt_surcharge_types"].ToString().Trim().ToUpper();
                }
            }

            btnAddSeg.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");

            txtOriAirport.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtOriAirport',event)");
            txtDesAirport.Attributes.Add("onkeyup", "auto_suggest('MainContent_txtDesAirport',event)");
            txtOriAirport.Attributes.Add("onkeydown", "key_down(event)");
            txtDesAirport.Attributes.Add("onkeydown", "key_down(event)");
            txtOriAirport.Attributes.Add("autocomplete", "off");
            txtDesAirport.Attributes.Add("autocomplete", "off");
        }

        protected void btnAddSeg_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FB5") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            lblError.Text = "";
            int ret_code;
            try
            {
                SqlCommand cmd = new SqlCommand("bll_surcharge_add_segment");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@code", lblSurchargeCode.Text);
                para = cmd.Parameters.AddWithValue("@flt_surcharge_types", lblFltSurchargeTypes.Text);
                para = cmd.Parameters.AddWithValue("@effective_time", lblEffectiveTime.Text);
                para = cmd.Parameters.AddWithValue("@ori_airport", txtOriAirport.Text.ToUpper());
                para = cmd.Parameters.AddWithValue("@des_airport", txtDesAirport.Text.ToUpper());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done";
                    grdSegments.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Adding failed!";
                else
                    lblError.Text = "Error: " + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FB5") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            lblError.Text = "";
            int ret_code;
            try
            {
                SqlCommand cmd = new SqlCommand("bll_surcharge_delete_segment");
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@code", lblSurchargeCode.Text);
                para = cmd.Parameters.AddWithValue("@flt_surcharge_types", lblFltSurchargeTypes.Text);
                para = cmd.Parameters.AddWithValue("@effective_time", lblEffectiveTime.Text);
                para = cmd.Parameters.AddWithValue("@ori_airport", txtOriAirport.Text);
                para = cmd.Parameters.AddWithValue("@des_airport", txtDesAirport.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done";
                    grdSegments.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Adding failed!";
                else
                    lblError.Text = "Error: " + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            grdSegments.DataBind();
        }
    }
}